
/*!
	\file
	\brief Main window's model.

		\author Igor Mironchik (igor.mironchik at gmail dot com).

		Copyright (c) 2010 - 2011 Igor Mironchik

	Permission is hereby granted, free of charge, to any person
	obtaining a copy of this software and associated documentation
	files (the "Software"), to deal in the Software without
	restriction, including without limitation the rights to use,
	copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following
	conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
	OTHER DEALINGS IN THE SOFTWARE.
*/

// eShopHelper include.
#include <eShopHelper/h/model.hpp>
#include <eShopHelper/h/goods.hpp>

// Qt include.
#include <QtCore/QString>
#include <QtGui/QPixmap>
#include <QtCore/QVector>


namespace eShopHelper {

namespace /* anonymous */ {

//
// Data
//

//! Data in the model.
struct Data {
	Data()
		:	m_price( 0.0 )
	{}

	Goods m_goods;
	QPixmap m_icon;
	QString m_name;
	double m_price;
	QString m_desc;
}; // struct Data

} /* namespace anonymous */

//
// Model::PImpl
//

struct Model::PImpl {
	QVector< Data > m_data;
}; // struct Model::PImpl

//
// Model
//

Model::Model( QObject * parent )
	:	QAbstractTableModel( parent )
	,	m_pimpl( new PImpl )
{
}

void
Model::clear()
{
	beginResetModel();
	m_pimpl->m_data.clear();
	endResetModel();
}

int
Model::rowCount( const QModelIndex & parent ) const
{
	return m_pimpl->m_data.size();
}

int
Model::columnCount( const QModelIndex & parent ) const
{
	return 4;
}

QVariant
Model::data( const QModelIndex & index, int role ) const
{
	const int column = index.column();

	if( role == Qt::DisplayRole )
	{
		switch( column )
		{
			case 1 : return m_pimpl->m_data[ index.row() ].m_name;
			case 2 : return QString::number(
				m_pimpl->m_data[ index.row() ].m_price, 'f', 2 );
			case 3 : return m_pimpl->m_data[ index.row() ].m_desc;
			default : return QVariant();
		}
	}
	else if( role == Qt::DecorationRole && column == 0 )
		return m_pimpl->m_data[ index.row() ].m_icon;
	else if( role == Qt::UserRole )
	{
		QVariant value;
		value.setValue( m_pimpl->m_data[ index.row() ].m_goods );
		return value;
	}
	else
		return QVariant();
}

bool
Model::insertRows( int row, int count, const QModelIndex & parent )
{
	Q_UNUSED( parent );
	beginInsertRows( QModelIndex(), row, row + count - 1 );
	m_pimpl->m_data.insert( row, count, Data() );
	endInsertRows();
	return true;
}

bool
Model::removeRows( int row, int count, const QModelIndex & parent )
{
	Q_UNUSED( parent );
	beginRemoveRows( QModelIndex(), row, row + count - 1 );
	m_pimpl->m_data.remove( row, count );
	endRemoveRows();
	return true;
}

bool
Model::setData( const QModelIndex & index, const QVariant & value, int role )
{
	const int column = index.column();
	const int row = index.row();

	if( role == Qt::DisplayRole )
	{
		switch( column )
		{
			case 1 : m_pimpl->m_data[ row ].m_name = value.toString();
				break;
			case 2 : m_pimpl->m_data[ row ].m_price = value.toDouble();
				break;
			case 3 : m_pimpl->m_data[ row ].m_desc = value.toString();
				break;
		}
	}
	else if( role == Qt::DecorationRole )
		m_pimpl->m_data[ row ].m_icon = value.value< QPixmap > ();
	else if( role == Qt::UserRole )
		m_pimpl->m_data[ row ].m_goods = value.value< Goods > ();

	emit dataChanged( QAbstractTableModel::index( index.row(), 0 ),
		QAbstractTableModel::index( index.row(), 3 ) );

	return true;
}

Qt::ItemFlags
Model::flags( const QModelIndex & index ) const
{
	if( index.column() == 0 )
		return ( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
	else
		return ( Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable );
}

QVariant
Model::headerData( int section, Qt::Orientation orientation, int role ) const
{
	if( role != Qt::DisplayRole )
		return QVariant();

	if( orientation == Qt::Horizontal )
	{
		switch ( section )
		{
			case 0 : return tr( "Image" );
			case 1 : return tr( "Name" );
			case 2 : return tr( "Price" );
			case 3 : return tr( "Description" );
			default : return QVariant();
		}
	}

	return QVariant();
}

} /* namespace eShopHelper */
